<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use think\facade\Filesystem;
use PhpOffice\PhpSpreadsheet\IOFactory;

class Tests extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {

        $id=671;

        $logs=Db::name('aa_case')->alias('a')
            ->join('cmf_quotaion_log b','a.q_id=b.quotation_id')
            ->where('a.id',$id)
            ->where('b.seesyatus','eq',1)
            ->field('b.*')
            ->select();


        $photo=Db::name('newphoto')->where('caseid',$id)->select();

        $alls=[];

        foreach ($logs as $index=>$keys){

            $all=[];
            $arrays=[];
            foreach ($photo as $key){
                $more=unserialize($key['more']);

                $array=[];
                if ($more[$index]['status']==0&&$more[$index]['set']=='是'){

                    $array['set']=$more[$index]['set'];
                    $array['code']=$key['code'];
                    $array['name']=$key['title']."_".$key['color1'];
                    $array['status']=$more[$index]['status'];

                    $arrays[]=$array;
                }

            }


            $all['title']=$keys['catefrolist_name'];
            $all['count']=count($arrays);
            $all['list']=$arrays;

            $alls[]=$all;


        }


        echo "<pre>";
        print_r($alls);
    }

    public function indexb()
{
    $id = 671;

    // 查询日志数据
    $logs = Db::name('aa_case')
        ->alias('a')
        ->join('cmf_quotaion_log b', 'a.q_id=b.quotation_id')
        ->join('cmf_catefrolist c','b.catefrolist_id=c.id')
        ->where('a.id', $id)
        ->where('b.seesyatus','eq',1)
        ->field('b.*,c.images as goodimages')
        ->select();

    // 查询照片数据
    $photo = Db::name('newphoto')->where('caseid', $id)->select();

    // 将照片数据按 caseid 分组
    $photoGroup = [];
    foreach ($photo as $key) {
        $caseid = $key['caseid'];
        if (!isset($photoGroup[$caseid])) {
            $photoGroup[$caseid] = [];
        }
        $photoGroup[$caseid][] = $key;
    }

    $alls = [];

    // 处理逻辑
    foreach ($logs as $index => $keys) {
        $all = [];
        $arrays = [];
        $arraysb=[];

        if (isset($photoGroup[$id])) {
            foreach ($photoGroup[$id] as $key) {
                try {
                    if (empty($key['more'])) {
                        throw new Exception("序列化数据为空");
                    }
                    $more = unserialize($key['more']);
                    if (!is_array($more) || !isset($more[$index]) || !isset($more[$index]['status']) || !isset($more[$index]['set'])) {
                        continue;
                    }
                        //拍摄
                    if ($more[$index]['status'] == 0 && $more[$index]['set'] == '是') {
                        $array = [
                            'set' => $more[$index]['set'],
                            'code' => $key['code'],
                            'name' => $key['title'] . "_" . $key['color1'],
                            'status' => $more[$index]['status']
                        ];


                        $arrays[] = $array;
                    }

                    if ($more[$index]['status'] == 1 && $more[$index]['set'] == '是') {
                        $array = [
                            'set' => $more[$index]['set'],
                            'code' => $key['code'],
                            'name' => $key['title'] . "_" . $key['color1'],
                            'status' => $more[$index]['status']
                        ];


                        $arraysb[] = $array;
                    }
                } catch (Exception $e) {
                    error_log("反序列化失败: " . $e->getMessage());
                }
            }
        }

        $all['title'] = $keys['catefrolist_name'];
        $all['count'] = count($arrays);
        $all['list1'] = $arrays;
        $all['list2'] = $arraysb;
        $all['goodimages'] =getallimages1($keys['goodimages']);
        $alls[] = $all;
    }

    // 输出结果
    echo "<pre>";
    print_r($alls);

}



}